package com.example.side_pull_menu.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.side_pull_menu.Fragmentkj3;
import com.example.side_pull_menu.R;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;
public class Fragment_5 extends Fragment {
    private BarChart barChart;
    private ArrayList<BarEntry> barEntries=new ArrayList<>();
    public Fragment_5() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_5, container, false);
        initData(view);
        initLineChart(view);
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable,1000);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
    public interface BlogService {
        @GET("aircondition")
        Call<ResponseBody> getBlog();
    }
    String[] name={
            "星期一",
            "星期二",
            "星期三",
            "星期四",
            "星期五",
            "星期六",
            "星期天",
    };
    private Handler handler=new Handler();
    private Runnable runnable=new Runnable() {
        @Override
        public void run() {
              setBarChartData(barChart);
              barChart.invalidate();
              handler.postDelayed(this,1000);
        }
    };
    private void initData(View view) {
        barChart =view.findViewById(R.id.check_bc);
        List<BarEntry> barEntries = new ArrayList<>();
        Log.i("TGA","run"+barEntries);
    }
    private void initLineChart(View view) {

        //柱状图的阴影
        barChart.setDrawBarShadow(false);
        //设置柱状图Value值显示在柱状图上方 true 为显示上方，默认false value值显示在柱状图里面
        barChart.setDrawValueAboveBar(true);
        //Description Label 是否可见
        barChart.getDescription().setEnabled(false);
        // 设置最大可见Value值的数量 针对于ValueFormartter有效果
        barChart.setMaxVisibleValueCount(10);
        // 二指控制X轴Y轴同时放大
        barChart.setPinchZoom(false);
        //是否显示表格背景颜色
        barChart.setDrawGridBackground(false);
        XAxis xAxis = barChart.getXAxis();
        //设置X轴显示位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //X轴纵向分割线，一般不设置显示
        xAxis.setDrawGridLines(false);
        // X轴显示Value值的精度，与自定义X轴返回的Value值精度一致
        xAxis.setGranularity(1f);
        //X轴横坐标显示的数量
        xAxis.setLabelCount(10);
        //X轴最大坐标
//        xAxis.setAxisMaximum(7.5f);
        //X轴最小坐标
        xAxis.setAxisMinimum(0.5f);
        //自定义x轴
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return name[(int) value];
            }
        });
        //Y左边轴
        YAxis leftAxis = barChart.getAxisLeft();
        //设置Y左边轴显示的值 label 数量
        leftAxis.setLabelCount(8, false);
        //设置值显示的位置，我们这里设置为显示在Y轴外面
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        //设置Y轴 与值的空间空隙 这里设置30f意为30%空隙，默认是10%
        leftAxis.setSpaceTop(30f);
        //设置Y轴最小坐标和最大坐标
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(10f);

        //Y轴右边轴的设置，跟左边轴类似
        YAxis rightAxis = barChart.getAxisRight();
        rightAxis.setDrawGridLines(false);
        //隐藏右边y轴
        rightAxis.setEnabled(false);
        rightAxis.setLabelCount(8, false);
        rightAxis.setSpaceTop(30f);
        rightAxis.setAxisMinimum(0f);
        rightAxis.setAxisMaximum(10f);
        // 设置表格标示的位置
        Legend l = barChart.getLegend();
        //标示坐落再表格左下方
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        //标示水平朝向
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //标示在表格外
        l.setDrawInside(false);
        //样式
        l.setForm(Legend.LegendForm.SQUARE);
        //字体
        l.setFormSize(15f);
        //大小
        l.setTextSize(15f);
    }
    public void setBarChartData(final BarChart barChart) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        RequestBody requestBody = (RequestBody) retrofit.create(BlogService.class);
        BlogService service = retrofit.create(BlogService.class);
        Call<ResponseBody> call = service.getBlog();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                barEntries = new ArrayList<>();
                try {
                    Gson gson1 = new Gson();
                    Fragmentkj3 carlist=gson1.fromJson(response.body().string(),Fragmentkj3.class);
                    for (int i=0;i<carlist.get拥挤程度().size();i++){
                        barEntries.add(new BarEntry(i,carlist.get拥挤程度().get(i)));
                        BarDataSet set1=new BarDataSet(barEntries,"一周天气湿度指数");
                        set1.setValueTextSize(10);
                        BarData set2=new BarData(set1);//创建数据
                        barChart.setData(set2);//设置数据
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}